package com.cogent.dynamicform.form.impl;

import java.util.HashMap;
import java.util.List;

import com.cogent.business.helper.HtmlUIHelper;
import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dao.BussinessQueryHelper;
import com.cogent.dynamicform.domain.BaseFormColumnDomain;
import com.cogent.dynamicform.domain.FormColumnDomain;
import com.cogent.dynamicform.form.AbstractDynamicForm;

public class HeadShipSelectDynamicFormImpl extends AbstractDynamicForm {
	
	public String genQueryField(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		StringBuffer str = new StringBuffer();
		str.append("<select name=\"").append(formColumnDomain.getName()).append("\" id=\"").append(formColumnDomain.getName()).append("\" ");
		if(!StringUtil.isEmpty(formColumnDomain.getJs()))
			str.append(formColumnDomain.getJs());
		str.append(">");
		str.append("<option value=\"\">请选择</option>");
		List headlist = BussinessQueryHelper.query("sys_title", "select * from sys_title where status=0 order by priority asc");
		int level = 1;
		String precode = "";
		String curdefaultValue = MapUtil.getStringValue(formMap, formColumnDomain.getName());
		for(int i=0; i<headlist.size(); i++) {
			HashMap map = (HashMap)headlist.get(i);
			if(i == 0)
				precode = MapUtil.getStringValue(map, "priority");
			else if(!precode.equals(MapUtil.getStringValue(map, "priority"))) {
				precode = MapUtil.getStringValue(map, "priority");
				level++;
			}
			str.append(HtmlUIHelper.getOption(MapUtil.getStringValue(map, "name"),
					MapUtil.getStringValue(map, "id"), curdefaultValue, level));
		}
		str.append("</select>");
				
		return str.toString();
	}
	
	public String genInputField(FormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getViewmodel() > 0)
			return getShowValue(formColumnDomain, formMap) + "&nbsp;";
		StringBuffer str = new StringBuffer();
		String className = "input_middle";
		if(!StringUtil.isEmpty(formColumnDomain.getShowclass()))
			className = formColumnDomain.getShowclass(); 
		String name = formColumnDomain.getName();
		if(name.indexOf(".") > 0)
			name = name.substring(name.indexOf(".") + 1);
		str.append("<select name=\"").append(formColumnDomain.getName()).append("\", id=\"").append(formColumnDomain.getName()).append("\" class=\"").append(className)
		 .append("\"  ");
		if(formColumnDomain.getJs().length()>0)
			str.append(formColumnDomain.getJs());
		str.append(" >");
		str.append("<option value=\"\">请选择</option>");
		List headlist = BussinessQueryHelper.query("sys_title", "select * from sys_title where status=0 order by priority asc");
		int level = 1;
		String precode = "";
		String curdefaultValue = MapUtil.getStringValue(formMap, name);
		for(int i=0; i<headlist.size(); i++) {
			HashMap map = (HashMap)headlist.get(i);
			if(i == 0)
				precode = MapUtil.getStringValue(map, "priority");
			else if(!precode.equals(MapUtil.getStringValue(map, "priority"))) {
				precode = MapUtil.getStringValue(map, "priority");
				level++;
			}
			str.append(HtmlUIHelper.getOption(MapUtil.getStringValue(map, "name"),
					MapUtil.getStringValue(map, "id"), curdefaultValue, level));
		}
		str.append("</select>");
		if(formColumnDomain.isIsrequired())
			str.append("<span class=\"txt_red\">&nbsp;*</span>");
		
		return str.toString();
	}
	
	public String getShowValue(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getShowtype() == 1)
			return "&nbsp;";
		String name = formColumnDomain.getName();
		if(name.indexOf(".") > 0)
			name = name.substring(name.indexOf(".") + 1);
		HashMap curMap = BussinessQueryHelper.getValueById("sys_title", MapUtil.getStringValue(formMap, "id"));
		
		return MapUtil.getStringValue(curMap, "name", "&nbsp;");
	}
}
